package com.zpark.interecptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器
 */
public class MyInterceptor implements HandlerInterceptor {
    /**
     * 拦截方法，在请求到达Controller之前进行拦截
     * 如果返回值为true则继续向下执行（访问controller资源）
     * 如果返回值为false则中断请求
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("请求到Controller之前  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> preHandle");
        return true;
    }

    /**
     * 请求到达Controller，但是在视图解析之前执行的方法
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("解析视图之前  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> postHandle");

    }

    /**
     * 请求完成之后，视图显然完成后执行的方法
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("请求执行完成  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> afterCompletion");
    }
}
